package org.nbict.iot.protocol.util;

import org.springframework.util.StringUtils;

import java.io.UnsupportedEncodingException;
import java.util.Collection;
import java.util.List;
import java.util.Locale;

/**
 * Created by zhuolin on 2018/4/9.
 */
public class BytesOperator {

    private static final String CHARSET = "UTF-8";


    public static boolean isEmpty(Collection collection) {
        return (collection == null || collection.isEmpty());
    }


    public static boolean isNotEmpty(String content){
        return StringUtils.hasText(content);
    }

    public static String bytesToHexString(byte[] data) {
        if (data == null || data.length == 0) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        for (byte b : data) {
            String hex = Integer.toHexString(b & 0xFF).toUpperCase(Locale.getDefault());
            sb.append(hex.length() < 2 ? "0" + hex : hex);
        }
        sb.setLength(sb.length());
        return sb.toString();
    }

    public static byte[] splitBytes(byte[] bytes,int from,int length){
        byte[] des = new byte[length];
        System.arraycopy(bytes, from, des, 0, length);
        return des;
    }

    public static byte[] splitFrontBytes(byte[] bytes,int from){
        return splitBytes(bytes, from, bytes.length - from);
    }

    public static String bytesToString(byte[] data){
        try {
            return new String(data,CHARSET);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static byte[] byteMerger(byte[] byte_1, byte[] byte_2){
        byte[] byte_3 = new byte[byte_1.length+byte_2.length];
        System.arraycopy(byte_1, 0, byte_3, 0, byte_1.length);
        System.arraycopy(byte_2, 0, byte_3, byte_1.length, byte_2.length);
        return byte_3;
    }

    public static byte[] hexStr2Bytes(String src) {
        if(src == null){
            return new byte[]{};
        }
        int m = 0, n = 0;
        int l = src.length() / 2;
        byte[] ret = new byte[l];
        for (int i = 0; i < l; i++) {
            m = i * 2 + 1;
            n = m + 1;
            ret[i] = uniteBytes(src.substring(i * 2, m), src.substring(m, n));
        }
        return ret;
    }

    private static byte uniteBytes(String src0, String src1) {
        byte b0 = Byte.decode("0x" + src0).byteValue();
        b0 = (byte) (b0 << 4);
        byte b1 = Byte.decode("0x" + src1).byteValue();
        byte ret = (byte) (b0 | b1);
        return ret;
    }

    public static byte[] bytesMerger(byte[]...bytes){
        int size = 0;
        for(byte[] byteUnit:bytes){
            size+=byteUnit.length;
        }
        byte[] totalBytes = new byte[size];
        int i = 0;
        for(byte[] byteUnit:bytes){
            System.arraycopy(byteUnit, 0, totalBytes, i, byteUnit.length);
            i+=byteUnit.length;
        }
        return totalBytes;
    }

    public static byte[] bytesMerger(List<byte[]> byteList){
        int size = 0;
        for(byte[] byteUnit:byteList){
            size+=byteUnit.length;
        }
        byte[] totalBytes = new byte[size];
        int i = 0;
        for(byte[] byteUnit:byteList){
            System.arraycopy(byteUnit, 0, totalBytes, i, byteUnit.length);
            i+=byteUnit.length;
        }
        return totalBytes;
    }

}
